﻿using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using RuleCode.Table;

namespace map.member.lvyou
{
    public partial class newviewpointinfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //bind user lyviewpoint
            if (!IsPostBack)
            {
                Fck_Content.Visible = false;
                SubmitEdit.Visible = false;
                GridViewBind();
            }
        }
        private void GridViewBind()
        {
            RuleCode.Table.lyviewpoint x = RuleCode.Table.lyviewpoint.Find("createuserid = '" + AliApi.appUserId + " '");
            this.GridView1.DataSource = x.MyTable;
            this.GridView1.DataBind();

        }
        private void DataListBind(string id)
        {
            lyviewpointimage image = lyviewpointimage.Find("lyviewpointrecid = '" + id+"'");
            this.DataList1.DataSource = image.MyTable;
            this.DataList1.DataBind();
            
        }
        private lyviewpoint biz;
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            Label1.Text = id;
            biz = lyviewpoint.Find(" recid = " + id , "RECID");
            despLabel.Text = biz.jcdesc1.ValueString;
            transparentLabel.Text = biz.jcdesc2.ValueString;
            DataListBind(id);
           
        }

        protected void editDesp_Click(object sender, EventArgs e)
        {
            textSource.Text = "jcdesc1";
            Edit(despLabel.Text);
        }

        protected void editTransparent_Click(object sender, EventArgs e)
        {
            textSource.Text = "jcdesc2";
            Edit(transparentLabel.Text);
        }
        private void Edit(string source)
        {
            Fck_Content.Visible = true;
            SubmitEdit.Visible = true;
            Fck_Content.Value = Server.HtmlDecode(source);

        }

        protected void SubmitEdit_Click(object sender, EventArgs e)
        {
            Fck_Content.Visible = false;
            SubmitEdit.Visible = false;
            biz = lyviewpoint.Find(" recid = '" + Label1.Text + "'", "RECID");

            if (textSource.Text == "jcdesc1")
            {
                biz.jcdesc1 = Fck_Content.Value;
                despLabel.Text = Fck_Content.Value;
            }
            if (textSource.Text == "jcdesc2")
            {
                biz.jcdesc2 = Fck_Content.Value;
                transparentLabel.Text = Fck_Content.Value;
            }
            biz.Modify();
        }

        protected void BTNUp_Click(object sender, EventArgs e)
        {
            //string root = Server.MapPath("~/");
            //string path = "pic/";
            //string mpath = path + FileUpload1.FileName;
            bool fileOK = false;

            if (FileUpload1.HasFile)
            {
                //返回文件的扩展名
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                //设置限制的文件类型
                String[] allowExtension = { ".jpg", ".gif", ".bmp" ,"png"};
                //判断用户选择的文件类型是否受限制
                for (int i = 0; i < allowExtension.Length; i++)
                {
                    if (fileExtension == allowExtension[i])
                    {
                        fileOK = true;
                    }
                }
                //大于1M的文件不能上传
                if (FileUpload1.PostedFile.ContentLength > 1024000)
                {
                    fileOK = false;
                }

            }
            string strImg2 = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(chatblog.Util.UploadImageDirectory) + time2;
            string linkpth = ResolveUrl(chatblog.Util.UploadImageDirectory) + time2;

            if (!fileOK)
            {
                Response.Write("<script>alert('图片格式不正确对或者超过1M！');</script>");
            }
            if ( fileOK)
            {
                FileUpload1.PostedFile.SaveAs(strFilePth);
                Response.Write("<script>alert('上传成功！');</script>");
                RuleCode.Table.lyviewpointimage images = lyviewpointimage.Find(" 1=2 ");
                images.New();
                images.lyviewpointrecid = Label1.Text;
                images.cbimgpath = linkpth;
                images.jcdesc = imageDespcripton.Text;
                images.Insert();
                DataListBind(Label1.Text);
                //Response.Redirect("newviewpointinfo.aspx");

            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if(e.CommandName =="delete")
            {
                string id = e.CommandArgument.ToString();
                RuleCode.Table.lyviewpointimage images = lyviewpointimage.Find(" RECID= " + id);
                //delete image file
               // string temp = ResolveUrl(images.cbimgpath.StringValue);
               //string te= Server.MapPath(temp);
                string fileName = Server.MapPath(ResolveUrl(images.cbimgpath.StringValue));
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                    images.Delete();
                    DataListBind(Label1.Text);
                }
               
               
              
                //e.CommandArgument 
            }
          
        }
       
    }
}
